using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using Common;
using Common.Enum;
using Controls;
using ProjectView.Windows;
using Report;
using SProject.Paramter;

namespace ProjectView.ProjectControls
{
	public partial class UCProjectBasicOption : System.Windows.Controls.UserControl, IComponentConnector
	{
		public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(UCProjectBasicOption), new PropertyMetadata(false));

		public static readonly DependencyProperty BasicOptionProperty = DependencyProperty.Register("BasicOption", typeof(BasicOption), typeof(UCProjectBasicOption), new PropertyMetadata(null, OnBasicOptionChanged));

		public DataGridEx GridControl
		{
			get
			{
				return grid;
			}
		}

		public bool IsReadOnly
		{
			get
			{
				return (bool)GetValue(IsReadOnlyProperty);
			}
			set
			{
				SetValue(IsReadOnlyProperty, value);
			}
		}

		public BasicOption BasicOption
		{
			get
			{
				return (BasicOption)GetValue(BasicOptionProperty);
			}
			set
			{
				SetValue(BasicOptionProperty, value);
			}
		}

		public List<KeyValuePair> AllAnalysisTypeList
		{
			get
			{
				return EnumManagement.GetAllAnalysisType();
			}
		}

		public List<KeyValuePair> AllOptionzationTypeList
		{
			get
			{
				return EnumManagement.GetAllOptionzationType();
			}
		}

		private static void OnBasicOptionChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			UCProjectBasicOption uCProjectBasicOption = sender as UCProjectBasicOption;
			if (uCProjectBasicOption != null)
			{
				uCProjectBasicOption.RefreshData();
			}
		}

		public UCProjectBasicOption()
		{
			InitializeComponent();
			grid.addMenu(new Separator());
			System.Windows.Controls.MenuItem menuItem = new System.Windows.Controls.MenuItem();
			menuItem.Header = Utility.GetMessage("Project_Memu_Data_Export");
			menuItem.Click += MenuExportData_Click;
			grid.IsLineCopy_Paste_Visable = true;
			grid.IsContextMenuVisible = true;
			grid.addMenu(menuItem);
			AddHandler(DataGridEx.OnCopyRowRoutedEvent, new RoutedEventHandler(OnCopyRowRouted_RoutedEvent), true);
			if (ConfigReader.GetInstance().GetShowStandardCurveParamter())
			{
				colParmaterA.Visibility = Visibility.Visible;
				colParmaterB.Visibility = Visibility.Visible;
			}
		}

		private void OnCopyRowRouted_RoutedEvent(object sender, RoutedEventArgs e)
		{
			CopyRowRoutedEventArgs copyRowRoutedEventArgs = e as CopyRowRoutedEventArgs;
			if (copyRowRoutedEventArgs == null)
			{
				return;
			}
			BasicOptionItem basicOptionItem = copyRowRoutedEventArgs.SourceItem as BasicOptionItem;
			BasicOptionItem basicOptionItem2 = copyRowRoutedEventArgs.TargetItem as BasicOptionItem;
			if (basicOptionItem != null && basicOptionItem2 != null)
			{
				basicOptionItem2.AnalysisType = basicOptionItem.AnalysisType;
				basicOptionItem2.BeginBaseline = basicOptionItem.BeginBaseline;
				basicOptionItem2.EndBaseline = basicOptionItem.EndBaseline;
				basicOptionItem2.OptimizationMode = basicOptionItem.OptimizationMode;
				basicOptionItem2.AutoThreshold = basicOptionItem.AutoThreshold;
				basicOptionItem2.Threshold = basicOptionItem.Threshold;
				basicOptionItem2.DigitalFilter = basicOptionItem.DigitalFilter;
				if (colParmaterA.Visibility == Visibility.Visible)
				{
					basicOptionItem2.ParamterA = basicOptionItem.ParamterA;
					basicOptionItem2.ParamterB = basicOptionItem.ParamterB;
				}
				e.Handled = true;
			}
		}

		private void RefreshData()
		{
			if (BasicOption != null)
			{
				grid.ItemsSource = BasicOption.Items;
				BasicOption.Items.CollectionChanged += Items_CollectionChanged;
				RefreshColumnTubeNameVisibility();
			}
			else
			{
				grid.ItemsSource = null;
			}
		}

		private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			RefreshColumnTubeNameVisibility();
		}

		private void RefreshColumnTubeNameVisibility()
		{
			if (BasicOption != null)
			{
				if (BasicOption.Items.Select((BasicOptionItem s) => s.TubeNo).Distinct().Count() <= 1 || BasicOption.Items.Where((BasicOptionItem s) => !string.IsNullOrEmpty(s.TubeName)).Count() == 0)
				{
					columnTubeName.Visibility = Visibility.Collapsed;
				}
				else
				{
					columnTubeName.Visibility = Visibility.Visible;
				}
			}
		}

		private void Command_Gain_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = true;
		}

		private void Command_Gain_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			ProjectGainInfoWindow projectGainInfoWindow = new ProjectGainInfoWindow();
			projectGainInfoWindow.setData(BasicOption.Items);
			projectGainInfoWindow.Owner = Utility.GetMainWindow();
			projectGainInfoWindow.ShowDialog();
		}

		private void grid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
		{
			if (IsReadOnly)
			{
				e.Cancel = true;
			}
			else if (e.Column.Header != null && e.Column.Header.ToString() == Utility.GetMessage("Prj_ManualThreshold"))
			{
				BasicOptionItem basicOptionItem = e.Row.Item as BasicOptionItem;
				if (basicOptionItem != null && basicOptionItem.AutoThreshold)
				{
					e.Cancel = true;
				}
			}
		}

		public void UnSelectAll()
		{
			grid.UnselectAllCells();
		}

		public bool Validate(int cycles, bool showMsg = true)
		{
			bool flag = true;
			DataGridColumn dataGridColumn = null;
			int index = -1;
			StringBuilder stringBuilder = new StringBuilder();
			if (showMsg)
			{
				grid.CommitEdit();
			}
			for (int i = 0; i < BasicOption.Items.Count; i++)
			{
				BasicOptionItem basicOptionItem = BasicOption.Items[i];
				index = i;
				string text = Utility.GetMessage("Target") + "[" + basicOptionItem.TargetName + "]:";
				if (cycles > 0 && basicOptionItem.EndBaseline > cycles)
				{
					stringBuilder.Clear();
					stringBuilder.Append(text + Utility.GetMessage("msg_endBaseline_should_less_than_count_of_fluorescence") + "(" + cycles + ")");
					dataGridColumn = ColumnEndBaseLine;
					flag = false;
					break;
				}
				if (basicOptionItem.BeginBaseline >= basicOptionItem.EndBaseline)
				{
					stringBuilder.Clear();
					stringBuilder.Append(text + Utility.GetMessage("msg_beginBaseline_should_less_than_endBaseline"));
					dataGridColumn = ColumnBeginBaseLine;
					flag = false;
					break;
				}
			}
			if (showMsg && !flag && dataGridColumn != null && stringBuilder.Length > 0)
			{
				grid.UnselectAllCells();
				System.Windows.MessageBox.Show(stringBuilder.ToString());
				grid.CurrentColumn = dataGridColumn;
				grid.CurrentItem = BasicOption.Items[index];
				grid.BeginEdit();
			}
			return flag;
		}

		private void MenuExportData_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Title = Utility.GetMessage("Save");
			saveFileDialog.Filter = string.Format("{1}{0}|*.{2}|{3}{0}|*.{3}|{4}{0}|*.{4}", Utility.GetMessage("File"), "Excel", "xlsx", "csv", "txt");
			saveFileDialog.FilterIndex = 1;
			saveFileDialog.InitialDirectory = Utility.GetMyDocumentFolder();
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				base.Cursor = System.Windows.Input.Cursors.Wait;
				switch (saveFileDialog.FilterIndex)
				{
				case 1:
					SaveToExcel(saveFileDialog.FileName);
					break;
				case 2:
				case 3:
					SaveToCsv(saveFileDialog.FileName);
					break;
				}
				base.Cursor = System.Windows.Input.Cursors.Arrow;
				System.Windows.MessageBox.Show(Utility.GetMessage("msg_data_export_sussess"));
			}
		}

		public void SaveToExcel(string file)
		{
			ExcelData excelData = new ExcelData();
			ExcelSheetData excelSheetData = new ExcelSheetData();
			excelSheetData.Name = Utility.GetMessage("Prj_BasicParameter");
			excelSheetData.Data = GetData(true);
			excelData.SheetDataList.Add(excelSheetData);
			ExcelOperator.SaveToExcel(file, excelData);
		}

		public void SaveToCsv(string file)
		{
			CsvTxtOperator.SaveToCsv(file, GetData());
		}

		private List<List<string>> GetData(bool includeMergerCellInfo = false)
		{
			List<List<string>> list = new List<List<string>>();
			List<string> list2 = new List<string>();
			foreach (DataGridColumn column in grid.Columns)
			{
				if (column.Visibility == Visibility.Visible)
				{
					list2.Add(column.Header.ToString());
				}
			}
			list.Add(list2);
			List<string> item = new List<string>();
			foreach (BasicOptionItem item2 in (IEnumerable)grid.Items)
			{
				list2 = new List<string>();
				list2.Add(item2.TargetName);
				list2.Add(item2.ChannelNo.ToString());
				list2.Add(EnumManagement.GetAnalysisTypeName(item2.AnalysisType));
				list2.Add(item2.BeginBaseline.ToString());
				list2.Add(item2.EndBaseline.ToString());
				list2.Add(EnumManagement.GetOptionzationTypeName(item2.OptimizationMode));
				list2.Add(item2.AutoThreshold ? "Yes" : "No");
				list2.Add(item2.Threshold.ToString("0.00"));
				KeyValuePair keyValuePair = (from s in EnumManagement.GetAllDigitalFilter()
					where (EDigitalFilter)s.Key == item2.DigitalFilter
					select s).FirstOrDefault();
				if (keyValuePair != null)
				{
					list2.Add(keyValuePair.Value.ToString());
				}
				else
				{
					list2.Add(string.Empty);
				}
				if (colParmaterA.Visibility == Visibility.Visible)
				{
					list2.Add(item2.ParamterA.ToString("0.00"));
					list2.Add(item2.ParamterB.ToString("0.00"));
				}
				list.Add(list2);
			}
			if (includeMergerCellInfo)
			{
				list.Insert(0, item);
			}
			return list;
		}
	}
}
